﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IndoorInformation
{
    public partial class frmEditSuKien : Form
    {
        protected DoiTuong _DoiTuong = null;
        protected EditState _EditState = EditState.None;
        public frmEditSuKien(InformationRoom InformationRoom)
        {
            InitializeComponent();
            _DoiTuong = InformationRoom;
        }

        private void frmEditPhongBan_Load(object sender, EventArgs e)
        {
            try
            {
                txtName.Select();
                if (_DoiTuong == null)
                {
                    _DoiTuong = DanhMucInformationRoom.GetAll().NewDoiTuong();
                    _EditState = EditState.CreatingNew;
                }
                else
                {
                    txtName.Text = (_DoiTuong as InformationRoom).RoomName;
                    try
                    {
                        dateStart.EditValue = (_DoiTuong as InformationRoom).StartDate;
                    }
                    catch (Exception) { }
                    try
                    {
                        dateEnd.EditValue = (_DoiTuong as InformationRoom).EndDate;
                    }
                    catch (Exception) { }
                    try
                    {
                        txtInfo.Text = (_DoiTuong as InformationRoom).Infor;
                    }
                    catch (Exception) { }

                    _EditState = EditState.Modifying;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                //Check infor

                //Lưu
                (_DoiTuong as InformationRoom).RoomName = txtName.Text;
                try
                {
                    if (dateStart.EditValue is DateTime)
                        (_DoiTuong as InformationRoom).StartDate = Convert.ToDateTime(dateStart.EditValue);
                }
                catch (Exception) { }
                try
                {
                    if (dateEnd.EditValue is DateTime)
                        (_DoiTuong as InformationRoom).EndDate = Convert.ToDateTime(dateEnd.EditValue);
                }
                catch (Exception) { }
                try
                {
                    (_DoiTuong as InformationRoom).Infor = txtInfo.Text;
                }
                catch (Exception) { }

                if (_EditState == EditState.CreatingNew)
                {
                    (_DoiTuong as InformationRoom).InsertToDB();
                }
                else
                {
                    (_DoiTuong as InformationRoom).UpdateToDB();
                }

                MessageBox.Show("Lưu thành công");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
